package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.DZfundsMapper;
import com.hbnu.system.model.entity.DZfunds;
import com.hbnu.system.model.entity.Tranproject_registration;
import com.hbnu.system.service.DZfundsService;
import com.hbnu.system.service.ITranproject_registrationService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.RegexPatterns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DZfundsServiceImpl extends ServiceImpl<DZfundsMapper, DZfunds> implements DZfundsService {
    @Autowired
    DZfundsService dZfundsService;
    @Autowired
    private ITranproject_registrationService iTranproject_registrationService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 添加到账经费
     * @param dzfunds
     * @return
     */
    @Override
    public boolean addDZfundsProject(DZfunds dzfunds) {
        //不使用前端传过来的主键
        dzfunds.setDzfundsID(null);
        //查询横向项目
        Tranproject_registration project = iTranproject_registrationService.getTranproject(dzfunds.getTregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        if(StrUtil.isEmpty(dzfunds.getTregistrationID()) || StrUtil.isBlank(dzfunds.getTregistrationID())){
            throw new BaseException("请输入横向项目id!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsNum()) || StrUtil.isBlank(dzfunds.getDzfundsNum())){
            throw new BaseException("请输入到账编号!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsAmount()) || StrUtil.isBlank(dzfunds.getDzfundsAmount())){
            throw new BaseException("请输入到账金额!");
        }
        //校验经费是否输入的是数字
        if (!dzfunds.getDzfundsAmount().matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        DZfunds dZfunds = dZfundsService.getOne(Wrappers.lambdaQuery(DZfunds.class)
                .eq(DZfunds::getDzfundsNum,dzfunds.getDzfundsNum()));
        if(!Objects.isNull(dZfunds)){
            throw new BaseException("到账编号已被使用");
        }
        if(StrUtil.isEmpty(dzfunds.getFundsSours()) || StrUtil.isBlank(dzfunds.getFundsSours())){
            throw new BaseException("请输入经费来源!");
        }
        if(Objects.isNull(dzfunds.getDzfundsDate())){
            throw new BaseException("请输入到账时间!");
        }
        dzfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.insert(dzfunds)==1;
    }

    /**
     * 修改到账经费
     * @param dzfunds
     * @return
     */
    @Override
    public boolean updateDZfundsProject(DZfunds dzfunds) {
        Tranproject_registration project = iTranproject_registrationService.getTranproject(dzfunds.getTregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());

        if(StrUtil.isEmpty(dzfunds.getTregistrationID()) || StrUtil.isBlank(dzfunds.getTregistrationID())){
            throw new BaseException("请输入横向项目id!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsNum()) || StrUtil.isBlank(dzfunds.getDzfundsNum())){
            throw new BaseException("请输入到账编号!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsAmount()) || StrUtil.isBlank(dzfunds.getDzfundsAmount())){
            throw new BaseException("请输入到账金额!");
        }
        //校验经费是否输入的是数字
        if (!dzfunds.getDzfundsAmount().matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        DZfunds dZfunds = dZfundsService.getOne(Wrappers.lambdaQuery(DZfunds.class)
                .eq(DZfunds::getDzfundsNum,dzfunds.getDzfundsNum())
                .ne(DZfunds::getDzfundsID,dzfunds.getDzfundsID()));
        if(!Objects.isNull(dZfunds)){
            throw new BaseException("到账编号已被使用");
        }
        if(StrUtil.isEmpty(dzfunds.getFundsSours()) || StrUtil.isBlank(dzfunds.getFundsSours())){
            throw new BaseException("请输入经费来源!");
        }
        if(Objects.isNull(dzfunds.getDzfundsDate())){
            throw new BaseException("请输入到账时间!");
        }
        dzfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.updateById(dzfunds)==1;
    }

    @Override
    public boolean removeDZfundsProject(String dzfundsID) {
        DZfunds dZfunds = super.getOne(Wrappers.lambdaQuery(DZfunds.class)
                .eq(DZfunds::getDzfundsID,dzfundsID));
        if(Objects.isNull(dZfunds)){
            throw new BaseException("不存在该到账经费");
        }
        Tranproject_registration project = iTranproject_registrationService.getTranproject(dZfunds.getTregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        return baseMapper.deleteById(dZfunds) == 1;


    }

    @Override
    public List<DZfunds> getDZfundsByTranprojectID(String tranprojectID) {
        Tranproject_registration tranproject_registration = iTranproject_registrationService.getById(tranprojectID);
        if(Objects.isNull(tranproject_registration)){
            throw new BaseException("输入的横向项目编号有误");
        }
        List<DZfunds> dzfunds = baseMapper.selectList(Wrappers.lambdaQuery(DZfunds.class)
                .eq(DZfunds::getTregistrationID,tranprojectID));
        return dzfunds;
    }


}
